package com.spdzen.support;

import javax.servlet.http.HttpServletRequest;

/**
 * Request工具类
 * @author accountwcx@qq.com
 *
 */
public class RequestUtils {
	private static final String HEADER_USER_AGENT = "User-Agent";
	
	/**
	 * 获取客户端IP
	 * @param request
	 * @return 客户端IP
	 */
	public static String getClientIp(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		
		if(ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		
		if(ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		
		if(ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
			ip = request.getRemoteAddr(); 
		}
		
		return ip;
	}
	
	/**
	 * 判断是否微信浏览器
	 * @param request
	 * @return
	 */
	public static boolean isWeixinBrowser(HttpServletRequest request) {
		String ua = request.getHeader(HEADER_USER_AGENT);
		if(ua != null && ua.toLowerCase().indexOf("micromessenger") > 0){
			return true;
		}
		
		return false;
	}
	
	/**
	 * 获取User-Agent
	 * @param request
	 * @return
	 */
	public static String getUserAgent(HttpServletRequest request) {
		return request.getHeader(HEADER_USER_AGENT);
	}
}
